package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.bean.specialist.qo.QueryDocTeamListReq;
import hcn.specialist.DiseaseSpecies;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.hcs.specialist.bean.request.QueryDiseaseSpeciesListReq;
import com.bsoft.gol.hcs.specialist.bean.request.QueryDiseaseTypeListReq;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.Constants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tongtp
 * @version v0.1
 * @className DiseaseSpeciesDAOen
 * @description 病种管理
 * @create 2020-07-18 10:33
 **/
@SsdevDao( serviceId = "diseaseSpeciesDAOService")
public abstract class DiseaseSpeciesDAO extends HibernateSupportDelegateDAO<DiseaseSpecies> {


    @DAOMethod(sql = " from DiseaseSpecies where department_id =:departmentId ")
    public abstract List<QueryDiseaseTypeListReq> queryDiseaseTypeList(@DAOParam("departmentId")Long departmentId);

    @DAOMethod(sql = " from DiseaseSpecies where status=1 ")
    public abstract List<DiseaseSpecies> queryAllDiseaseTypeList();

    @DAOMethod(sql = " from DiseaseSpecies where disease_type_id =:diseaseTypeId  ")
    public abstract DiseaseSpecies getDiseaseTypeDetail(@DAOParam("diseaseTypeId")Long diseaseTypeId);

    @DAOMethod(sql = " from DiseaseSpecies where department_id =:departmentId and  diseaseTypeName=:diseaseTypeName ")
    public abstract List<DiseaseSpecies> queryDiseaseTypeByName(@DAOParam("departmentId")String departmentId,@DAOParam("diseaseTypeName")String diseaseTypeName);

    @DAOMethod(sql = " from DiseaseSpecies where department_id =:departmentId and status=1")
    public abstract List<DiseaseSpecies> queryChronicDiseaseList( @DAOParam("departmentId")String departmentId);

    @DAOMethod(sql = " from DiseaseSpecies where organization_id =:organizationId and status=1")
    public abstract List<DiseaseSpecies> queryChronicDiseaseListByOrg( @DAOParam("organizationId")String organizationId);

    @DAOMethod(sql = " from DiseaseSpecies where status=1")
    public abstract List<DiseaseSpecies> queryAllChronicDiseaseList();


    @RpcService
    public List<DiseaseSpecies> queryDiseaseSpeciesListByOrdAndDept(final QueryDocTeamListReq request) {

        HibernateStatelessResultAction<List<DiseaseSpecies>> action =
                new AbstractHibernateStatelessResultAction<List<DiseaseSpecies>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append(" select disease_type_id as diseaseTypeId,organization_id as organizationId," +
                                "disease_type_name as diseaseTypeName,department_id as departmentId,department_name " +
                                "as departmentName,department_code as departmentCode,status as status,gmt_create as gmtCreate, " +
                                " pinyinCode,abbreviation,periodViewDispFlag from fus_disease_species_record where 1=1 ");

                        if (CommonUtils.isNotEmpty(request.getOrganizationId())) {
                            sqlBuilder.append(" and organization_id = :organizationId ");
                        }
                        if (CommonUtils.isNotEmpty(request.getDepartmentId())) {
                            sqlBuilder.append(" and department_id = :departmentId ");
                        }

                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        if (CommonUtils.isNotEmpty(request.getOrganizationId())) {
                            query.setParameter("organizationId", request.getOrganizationId());
                        }

                        if (CommonUtils.isNotEmpty(request.getDepartmentId())) {
                            query.setParameter("departmentId", request.getDepartmentId());
                        }
                        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                        List<Map> list = query.list();
                        List<DiseaseSpecies> voList = new ArrayList<>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, DiseaseSpecies.class));
                            }
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();

    }
    /**
     * @param request
     * @throws
     * @description: 获取病种记录数
     * @return: java.lang.Long
     * @author: liwenlong
     * @date: 2020/7/31 9:51
     */
    public List<DiseaseSpecies> queryDiseaseSpeciesList(final QueryDiseaseSpeciesListReq request) {

        HibernateStatelessResultAction<List<DiseaseSpecies>> action =
                new AbstractHibernateStatelessResultAction<List<DiseaseSpecies>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append(" select disease_type_id as diseaseTypeId,organization_id as organizationId," +
                                "disease_type_name as diseaseTypeName,department_id as departmentId,department_name " +
                                "as departmentName,department_code as departmentCode,status as status,gmt_create as gmtCreate, " +
                                " pinyinCode,abbreviation,periodViewDispFlag from fus_disease_species_record where 1=1 ");

                        if (null != request.getOrganizationId()) {
                            sqlBuilder.append(" and organization_id = :organizationId ");
                        }
                        if (CommonUtils.isNotEmpty(request.getDiseaseTypeId())) {
                            sqlBuilder.append(" and disease_type_id = :diseaseTypeId ");
                        }
                        if (CommonUtils.isNotEmpty(request.getDepartmentId())) {
                            sqlBuilder.append(" and department_id = :departmentId ");
                        }
                        if (CommonUtils.isNotEmpty(request.getStatus())){
                            sqlBuilder.append(" and status = :status ");
                        }

                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        if (null != request.getOrganizationId()) {
                            query.setParameter("organizationId", request.getOrganizationId());
                        }
                        if (CommonUtils.isNotEmpty(request.getDiseaseTypeId())) {
                            query.setParameter("diseaseTypeId", request.getDiseaseTypeId());
                        }

                        if (CommonUtils.isNotEmpty(request.getDepartmentId())) {
                            query.setParameter("departmentId", request.getDepartmentId());
                        }
                        if (CommonUtils.isNotEmpty(request.getStatus())){
                            query.setParameter("status",request.getStatus());
                        }

                        query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                        //分页
                        if(Constants.ERR_CODE != request.getPageNo()){
                            int first = request.getPageNo() > 0 ? (request.getPageNo() - 1) * request.getPageSize() : 0;
                            query.setFirstResult(first);
                            query.setMaxResults(request.getPageSize());
                        }
                        List<Map> list = query.list();
                        List<DiseaseSpecies> voList = new ArrayList<>();
                        if (CommonUtils.isNotEmpty(list)) {
                            for (Map map : list) {
                                voList.add(ConversionUtils.convert(map, DiseaseSpecies.class));
                            }
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();

    }
    /**
     * @param request
     * @throws
     * @description: 获取病种记录总记录数
     * @return: java.lang.Long
     * @author: liwenlong
     * @date: 2020/7/31 9:51
     */
    public Long queryDiseaseSpeciesCount(final QueryDiseaseSpeciesListReq request) {
        HibernateStatelessResultAction<Long> action = new AbstractHibernateStatelessResultAction<Long>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append(" select count(1) countValue from fus_disease_species_record where 1=1 ");

                if (null != request.getOrganizationId()) {
                    sqlBuilder.append(" and organization_id = :organizationId ");
                }
                if (CommonUtils.isNotEmpty(request.getDiseaseTypeId())) {
                    sqlBuilder.append(" and disease_type_id = :diseaseTypeId ");
                }
                if (CommonUtils.isNotEmpty(request.getDepartmentId())) {
                    sqlBuilder.append(" and department_id = :departmentId ");
                }
                if (CommonUtils.isNotEmpty(request.getStatus())){
                    sqlBuilder.append(" and status = :status ");
                }

                Query query = ss.createSQLQuery(sqlBuilder.toString());
                if (null != request.getOrganizationId()) {
                    query.setParameter("organizationId", request.getOrganizationId());
                }
                if (CommonUtils.isNotEmpty(request.getDiseaseTypeId())) {
                    query.setParameter("diseaseTypeId", request.getDiseaseTypeId());
                }

                if (CommonUtils.isNotEmpty(request.getStatus())){
                    query.setParameter("status",request.getStatus());
                }

                if (CommonUtils.isNotEmpty(request.getDepartmentId())) {
                    query.setParameter("departmentId", request.getDepartmentId());
                }
                query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                List<Map> dbList = query.list();

                if (CommonUtils.isNotEmpty(dbList)) {
                    setResult(Long.valueOf(String.valueOf(dbList.get(0).get("countValue"))));
                } else {
                    setResult(0L);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    @DAOMethod(sql = " update  DiseaseSpecies set status=:status where diseaseTypeId =:diseaseTypeId")
    public abstract void updateById(@DAOParam("diseaseTypeId") Long diseaseTypeId,@DAOParam("status") Byte status);


    @DAOMethod(sql = "delete from DiseaseSpecies where diseaseTypeId =:diseaseTypeId")
    public abstract void deleteBydiseaseTypeId(@DAOParam("diseaseTypeId") Long diseaseTypeId);

    @DAOMethod(sql = " from DiseaseSpecies where organizationId =:organizationId and status=1")
    public abstract List<DiseaseSpecies> findDiseaseSpeciesListByOrgId(String organizationId);
}
